/**
 * GTHttpRequestParameterScanner.m
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */
#import "GTHttpRequestScanner.h"
#import "GTHttpParameter.h"

@interface GTHttpRequestScanner ()
@property(nonatomic,retain,readwrite) NSMutableSet *parametersToDetect;
@property(nonatomic,retain,readwrite) id target;
@property(nonatomic,assign,readwrite) SEL action;
@end

@implementation GTHttpRequestScanner
@synthesize parametersToDetect, target, action;

+ (id)scanner {
  return [[[self alloc] init] autorelease];
}

- (id)init {
  if ((self = [super init])) {
    self.parametersToDetect = [NSMutableSet set];
  }
  return self;
}

- (void)dealloc {
  [parametersToDetect release];
  [super dealloc];
}

- (void)detectParameter:(NSString*)aParameter {
  [self.parametersToDetect addObject:aParameter];
}

- (NSMutableDictionary*)scanRequest:(NSURLRequest*)aRequest {
  NSMutableDictionary *result = [NSMutableDictionary dictionary];
  NSString *parametersAsString = [[[NSString alloc] initWithData:[aRequest HTTPBody] encoding:NSASCIIStringEncoding] autorelease];
  NSScanner *scannerToSplitParamters = [NSScanner scannerWithString:parametersAsString];
  [scannerToSplitParamters setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"&"]];
  NSString *parameter = nil;
  while ([scannerToSplitParamters scanUpToString:@"&" intoString:&parameter] || [scannerToSplitParamters scanUpToString:@"" intoString:&parameter]) {
    NSScanner *scannerToSplitNameAndValue = [NSScanner scannerWithString:parameter];
    [scannerToSplitNameAndValue setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"="]];
    NSString *name = nil;
    if ([scannerToSplitNameAndValue scanUpToString:@"=" intoString:&name] && [self.parametersToDetect member:name]) {
      name = [name stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
      NSString *value = nil;
      [scannerToSplitNameAndValue scanUpToString:@"" intoString:&value];
      value = [value stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
      [result setValue:[GTHttpParameter parameterWithName:name value:value] forKey:name];
    }
  }
  if (self.target && self.action && [result count] > 0) {
    [self.target performSelector:self.action withObject:result];
  }
  return result;
}

- (void)notifyDetectedParametersTarget:(id)aTarget action:(SEL)anAction {
  self.target = aTarget;
  self.action = anAction;
}

- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType {
  [self scanRequest:aRequest];
  return TRUE;
}

- (void)webViewDidStartLoad:(UIWebView *)aWebView {
}

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
}

- (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error {
}

@end
